package test.framework;

import fox.framework.rpc.client.RpcRegister;
import fox.framework.rpc.client.node.rule.RoundRobinRuleFactory;
import fox.framework.rpc.client.node.rule.RuleFactory;
import fox.framework.rpc.client.register.ZookeeperRegister;
import fox.framework.rpc.context.DefaultRpcApplicationContext;
import fox.framework.rpc.context.RpcApplicationConfigureAdapter;
import fox.framework.rpc.context.RpcApplicationContext;
import fox.framework.rpc.context.RpcDefinitionRegister;
import fox.framework.rpc.context.definition.AbstractRpcDefinition;
import fox.framework.rpc.context.definition.RpcDefinitionBuilder;
import fox.framework.rpc.context.generator.name.RpcNameGenerator;
import fox.framework.rpc.context.processor.RpcDefinitionRegisterProcessor;
import test.framework.service.ServiceA;
import test.framework.service.ServiceAImpl;

/**
 * @author cuichao
 * @Description: Server
 * @Date: create in 2021/2/4 21:02
 */
public class Server {

    public static void main(String[] args) {

        ServerConfig config = new ServerConfig();
        DefaultRpcApplicationContext application = new DefaultRpcApplicationContext(config);

        try {
            application.run();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("服务端启动");
    }



    public static class ServerConfig extends RpcApplicationConfigureAdapter {

        @Override
        public String getGlobalConverterName() {
            return "testServerApp";
        }

        @Override
        public String getGlobalProtocol() {
            return "fox";
        }

        @Override
        public int getPort() {
            return 9999;
        }

        @Override
        public String getBind() {
            return "127.0.0.1";
        }

        @Override
        public RuleFactory getGlobalRuleFactory() {
            return new RoundRobinRuleFactory();
        }

        @Override
        public RpcRegister getRegister() {
            return new ZookeeperRegister();
        }

        @Override
        protected RpcDefinitionRegisterProcessor addDefinitionRegisterProcessor() {
            return new RpcDefinitionRegisterProcessor() {
                @Override
                public void registerRpcDefinition(RpcNameGenerator generator, RpcDefinitionRegister register) {

                    RpcDefinitionBuilder builder = RpcDefinitionBuilder.genericRpcDefinition();
                    builder.setMetaspace("ServiceA");
                    builder.setServiceInterface(ServiceA.class);
                    builder.setProvider(true);
                    AbstractRpcDefinition definition = builder.getDefinition();
                    String name = generator.generateRpcName(definition, register);
                    register.register(name,definition, new ServiceAImpl());
                }

                @Override
                public void process(RpcApplicationContext context) {
                    //nothing to do
                }

            };
        }
    }

}
